﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using View.EventArguments;
using View.Interfaces;

namespace View.Login
{
    public partial class UserLoginControl : ILoginPanel
    {
        protected enum MessageStateEnum
        {
            Empty,
            CapsLockWarning,
            LoginFail,
            Wait,
            NoCompletedForm
        } ;

        public event EventHandler<TryLoginEventAgrs> TryLoginEvent;
        public event EventHandler CallHelpEvent;
        
        private MessageStateEnum _messageState;
        
        public UserLoginControl()
        {
            InitializeComponent();
            UsernameTextBox.Focus();
            _messageState = MessageStateEnum.Empty;
            MessageLabel.Content = "";
        }

        private void UsernameTextBoxTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        { MessageState = MessageStateEnum.Empty; }

        private void UserpasswordBoxPasswordChanged(object sender, RoutedEventArgs e)
        {
            MessageState = !Console.CapsLock ? MessageStateEnum.Empty : MessageStateEnum.CapsLockWarning;
        }

        private void SubmitButtonClick(object sender, RoutedEventArgs e)
        {
            if(UsernameTextBox.Text.Length == 0 || UserpasswordBox.Password.Length == 0)
            {
                MessageState = MessageStateEnum.NoCompletedForm;
                return;
            }
            
            Dispatcher.Invoke(DispatcherPriority.Send, (ThreadStart)delegate
            { MessageState = MessageStateEnum.Wait; });

            Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, (ThreadStart)(() =>
                TryLoginEvent(this, new TryLoginEventAgrs(UsernameTextBox.Text, UserpasswordBox.Password))));
        }

        private void CallHelpClick(object sender, RoutedEventArgs e)
        { if(CallHelpEvent != null) CallHelpEvent(this, new EventArgs()); }

        private void GridKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.CapsLock:
                    if (UserpasswordBox.IsFocused && Console.CapsLock)
                        MessageState = MessageStateEnum.CapsLockWarning;
                    else
                        MessageState = MessageStateEnum.Empty;
                    break;

                case Key.F1:
                    if (CallHelpEvent != null) CallHelpEvent(this, new EventArgs());
                    break;

                case Key.Enter:
                    if (UsernameTextBox.Text.Length == 0 || UserpasswordBox.Password.Length == 0)
                    {
                        MessageState = MessageStateEnum.NoCompletedForm;
                        return;
                    }

                    Dispatcher.Invoke(DispatcherPriority.Send, (ThreadStart) delegate { MessageState = MessageStateEnum.Wait; });

                    Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, (ThreadStart) (() =>
                        TryLoginEvent(this, new TryLoginEventAgrs(UsernameTextBox.Text, UserpasswordBox.Password))));
                    break;
            }
        }

        private MessageStateEnum MessageState
        {
            set
            {
                if (MessageLabel.Content == null)
                {
                    _messageState = MessageStateEnum.Empty;
                    MessageLabel.Content = "";
                }
                if (_messageState == value) return;
                switch(value)
                {
                    case MessageStateEnum.Empty:
                        MessageLabel.Content = "";
                        break;

                    case MessageStateEnum.CapsLockWarning:
                        MessageLabel.Foreground = Brushes.Gold;
                        MessageLabel.Content = "Внимательно: включен CapsLock";
                        break;

                    case MessageStateEnum.LoginFail:
                        IsEnabled = true;
                        MessageLabel.Foreground = Brushes.Red;
                        MessageLabel.Content = "Ошибка входа: неверное имя или пароль";
                        break;

                    case MessageStateEnum.Wait:
                        IsEnabled = false;
                        MessageLabel.Foreground = Brushes.Black;
                        MessageLabel.Content = "Проверка учётной записи..";
                        break;

                    case MessageStateEnum.NoCompletedForm:
                        MessageLabel.Foreground = Brushes.Red;
                        MessageLabel.Content = "Для входа в систему заполните все поля";
                        break;
                }
                _messageState = value;
            }
        }

        private void UserpasswordBoxGotFocus(object sender, RoutedEventArgs e)
        { if (Console.CapsLock) MessageState = MessageStateEnum.CapsLockWarning; }

        private void UsernameTextBoxGotFocus(object sender, RoutedEventArgs e)
        { MessageState = MessageStateEnum.Empty; }

        public void ShowMessageLoginFail()
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
            { MessageState = MessageStateEnum.LoginFail; });
        }
    }
}